import {Injectable, BadRequestException} from '@nestjs/common';
import {CreateMeetingDto} from './dto/create-meeting.dto';
import {UpdateMeetingDto} from './dto/update-meeting.dto';
import {Meeting} from './entities/meeting.entity';
import {InjectRepository} from '@nestjs/typeorm';
import {Like, Repository} from 'typeorm';
import {MeetingListVo} from './vo/list.vo';

@Injectable()
export class MeetingService {
    @InjectRepository(Meeting)
    private repository: Repository<Meeting>;

    // 初始化数据
    initData() {
        const room1 = new Meeting();
        room1.name = '木星';
        room1.capacity = 10;
        room1.equipment = '白板';
        room1.location = '一层西';

        const room2 = new Meeting();
        room2.name = '金星';
        room2.capacity = 5;
        room2.equipment = '';
        room2.location = '二层东';

        const room3 = new Meeting();
        room3.name = '天王星';
        room3.capacity = 30;
        room3.equipment = '白板，电视';
        room3.location = '三层东';

        const room4 = new Meeting();
        room4.name = '会议室一';
        room4.capacity = 10;
        room4.equipment = '白板';
        room4.location = '一层西';

        const room5 = new Meeting();
        room5.name = '会议室二';
        room5.capacity = 5;
        room5.equipment = '';
        room5.location = '二层东';

        const room6 = new Meeting();
        room6.name = '大会议室';
        room6.capacity = 30;
        room6.equipment = '白板，电视';
        room6.location = '三层东';

        this.repository.save([room1, room2, room3, room4, room5, room6]);
    }

    // 列表
    async findAll(
        pageNum: number,
        pageSize: number,
        name: string,
        capacity: number,
        equipment: string,
    ) {
        const skipCount = (pageNum - 1) * pageSize;
        const condition: Record<string, any> = {};
        if (name) {
            condition.name = Like(`%${name}%`);
        }
        if (equipment) {
            condition.equipment = Like(`%${equipment}%`);
        }
        if (capacity) {
            condition.capacity = capacity;
        }

        const [meeting, total] = await this.repository.findAndCount({
            where: condition,
            select: [
                'id',
                'name',
                'capacity',
                'location',
                'equipment',
                'description',
                'isBooked',
                'createTime',
            ],
            skip: skipCount,
            take: pageSize,
        });

        const vo = new MeetingListVo();

        vo.meeting = meeting;
        vo.pageNum = pageNum;
        vo.pageSize = pageSize;
        vo.total = total;
        return vo;
    }

    // 新增
    async create(createMeetingDto: CreateMeetingDto) {
        const room = await this.repository.findOneBy({
            name: createMeetingDto.name,
        });

        if (room) {
            throw new BadRequestException('会议室名字已存在');
        }

        await this.repository.save(createMeetingDto);

        return 'success';
    }

    // 修改
    async update(updateMeetingDto: UpdateMeetingDto) {
        const meetingRoom = await this.repository.findOneBy({
            id: updateMeetingDto.id,
        });

        if (!meetingRoom) {
            throw new BadRequestException('会议室不存在');
        }

        meetingRoom.capacity = updateMeetingDto.capacity;
        meetingRoom.location = updateMeetingDto.location;
        meetingRoom.name = updateMeetingDto.name;

        if (updateMeetingDto.description) {
            meetingRoom.description = updateMeetingDto.description;
        }
        if (updateMeetingDto.equipment) {
            meetingRoom.equipment = updateMeetingDto.equipment;
        }

        await this.repository.update(
            {
                id: meetingRoom.id,
            },
            meetingRoom,
        );
        return 'success';
    }

    // 详情
    async findOne(id: number) {
        const details = this.repository.findOneBy({
            id,
        });
        return details;
    }

    // 删除
    async remove(id: number) {
        await this.repository.delete({
            id,
        });
        return 'success';
    }
}
